package com.example.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.SysMQMessageMapper;
import com.example.po.SysMQMessage;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @className: SysMQMessageServiceImpl
 * @author: chentao
 * @date: 2025/2/28 下午5:18
 * @Version: 1.0
 * @description:
 */
@Service
public class SysMQMessageServiceImpl extends ServiceImpl<SysMQMessageMapper, SysMQMessage> implements SysMQMessageService {

    /****
     * 查询最大范围的待消息信息
     * @param maxSize
     * @return
     * @throws Exception
     */
    @Override
    public List<SysMQMessage> getSysMQMessage(Long serverKey, int maxSize, int maxRetryCount) {
        return getBaseMapper().getSysMQMessage(serverKey, maxSize, maxRetryCount);
    }

    /****
     * 查询目前节点还有多少未执行的任务
     * @param serverKey
     * @return
     */
    @Override
    public int getSysMQMessageTotalSize(Long serverKey) {
        return getBaseMapper().getSysMQMessageTotalSize(serverKey);
    }


    /***
     * 把消息做负载转移
     * @param sourceServerKey
     * @param targetServerKey
     * @throws Exception
     */
    @Override
    public void updateMessageServer(Long sourceServerKey, Long targetServerKey) {
        getBaseMapper().updateMessageServer(sourceServerKey, targetServerKey);
    }

    /***
     * 把待分配的任务做一次分配
     * @param targetServerKey
     * @param countSize
     */
    @Override
    public void updateMessageServer(Long targetServerKey, int countSize) {
        getBaseMapper().updateMessageServerBatch(targetServerKey, countSize);
    }


    /****
     * 分片处理任务
     * @param sourceServerKey 掉线服务的key
     * @param targetServerKey 转移服务的key
     * @param index 批次
     * @param total 总节点数
     */
    @Override
    public void updateMessageServerBatch(Long sourceServerKey, Long targetServerKey, int index, int total) {
        getBaseMapper().updateAllMessageServerBatch(sourceServerKey, targetServerKey, index, total);
    }

    /***
     * 查询当前节点那些还未完成的任务的节点key
     * @param activeServerKeys
     * @return
     */
    @Override
    public List<Long> getDeathMessageServerExcept(List<Long> activeServerKeys) {
        return getBaseMapper().getMessageServerKeys(activeServerKeys);
    }

    /***
     * 查询所有未完成的任务节点信息
     * @return
     */
    @Override
    public List<Long> getAllDeathMessageServer() {
        return getBaseMapper().getAllMessageServer();
    }
}
